package cn.zhku.thing.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.itcast.jdbc.TxQueryRunner;
import cn.zhku.news.domain.News;
import cn.zhku.thing.domain.Thing;

public class ThingDao {
	private QueryRunner qr = new TxQueryRunner();

	public void add(Thing thing) {
		try {
			String sql = "insert into thing values(?,?,?,?,?,?,?,?,?,?)";
			Object[] params = {thing.getTid(),thing.getUserId(),thing.getTitle(),thing.getPrice(),
					thing.getImage(),thing.getDetail(),thing.getDate(),thing.getQq(),
					thing.getPhone(),thing.getPhone()
			};
					
		qr.update(sql, params);
		} catch(SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

	public ArrayList<Thing> findAll() {
		try {
			String sql = "select * from thing";
		return (ArrayList<Thing>) qr.query(sql,new BeanListHandler<Thing>(Thing.class));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

	public void edit(Thing thing) {
		try {
			String sql = "update thing set  values(?,?,?,?,?,?,?,?,?,?)";
			Object[] params = {thing.getTid(),thing.getUserId(),thing.getTitle(),thing.getPrice(),
					thing.getImage(),thing.getDetail(),thing.getDate(),thing.getQq(),
					thing.getPhone(),thing.getPhone()
			};
					
		qr.update(sql, params);
		} catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public void delete(Thing thing) {
		try {
			String sql = "delete from thing where tid = ?";
			qr.update(sql,thing.getTid());
		} catch (SQLException e) {
			throw new  RuntimeException(e);
		}
		
	}
}
